package com.zhixiang.xaec.action.system;


import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.google.gson.Gson;
import com.zhixiang.core.command.QueryFilter;
import com.zhixiang.core.entity.TreeObj;
import com.zhixiang.core.util.String2Array;
import com.zhixiang.core.web.action.BaseAction;
import com.zhixiang.xaec.entity.system.Forder;
import com.zhixiang.xaec.service.system.ForderService;


@Controller
@Scope("prototype")
public class ForderAction extends BaseAction
{

    private static final long serialVersionUID = -2845760327552708298L;

    private Forder forder;

    @Autowired
    private ForderService forderService;

    public String save()
    {
        forder.setRealName(System.currentTimeMillis() + "");
        forderService.saveForder(forder);
        return SUCCESS;
    }

    public String get()
    {
        if (forder != null && StringUtils.isNotEmpty(forder.getForId().toString()))
        {
            setResult(forderService.get(forder.getForId()));
        }
        return SUCCESS;
    }

    public String list()
    {
        QueryFilter localQueryFilter = new QueryFilter(getRequest());
        List<Forder> list = forderService.getAll(localQueryFilter);
        setTotalCounts(localQueryFilter.getPagingBean().getTotalItems());
        setResult(list);
        return SUCCESS;
    }

    public void getAll()
    {
        List<TreeObj> list = forderService.getTreeObj(1L);
        // 直接向页面输出字符串
        outJsonString(new Gson().toJson(list));
    }

    public String delete()
    {
        try
        {
            String ids = getRequest().getParameter("ids");
            Long[] l = String2Array.stringToLongArray(ids);
            forderService.remove(l);
        }
        catch (Exception e)
        {
            setSuccess(false);
            setMsg("删除文件夹失败");
        }

        return SUCCESS;
    }

    public Forder getForder()
    {
        return forder;
    }

    public void setForder(Forder forder)
    {
        this.forder = forder;
    }

}
